//Will Zepeda
//GSP-362
//Light.cpp
#include "Light.h"
#include "ENUM.h"
#include "DirectX.h"
#include "input.h"

Light::Light()
{
	//clear out light
	ZeroMemory(&mLight, sizeof(mLight));

	ambColor  = D3DXCOLOR(0.0f,0.0f,0.0f,0.0f);
	difColor  = D3DXCOLOR(0.0f,0.0f,0.0f,0.0f);
	specColor = D3DXCOLOR(0.0f,0.0f,0.0f,0.0f);
	mRange	  = 0.0f;

	//Value that specifies how light intensity changes over distance.
	//mess with these values to see how it affects lighting over distance
	mLight.Attenuation0 = 0.0f;
	mLight.Attenuation1 = 1.0f;//0.05f;
	mLight.Attenuation2 = 0.0f;
}
Light::~Light()
{

}
void Light::setLightType(int aType)
{
	switch(aType)
	{
	case POINT_LIGHT:	mLight.Type = D3DLIGHT_POINT;		break;
	case SPOT_LIGHT:	mLight.Type = D3DLIGHT_SPOT;		break;
	case DIR_LIGHT:		mLight.Type = D3DLIGHT_DIRECTIONAL;	break;
	}
}
void Light::setAmbientLight(D3DXCOLOR aColor)
{
	mLight.Ambient = aColor;
}
void Light::setAmbientLight(float aR, float aG, float aB, float aA)
{
	ambColor.r = aR; ambColor.g = aG; ambColor.b = aB; ambColor.a = aA;
	mLight.Ambient = D3DXCOLOR(aR, aG, aB, aA);	
}
void Light::setDiffuseLight(D3DXCOLOR aColor)
{
	mLight.Diffuse = aColor;
}
void Light::setDiffuseLight(float aR, float aG, float aB, float aA)
{
	difColor.r = aR; difColor.g = aG; difColor.b = aB; difColor.a = aA;
	mLight.Diffuse = D3DXCOLOR(aR, aG, aB, aA);
}
void Light::setSpecularLight(D3DXCOLOR aColor)
{
	mLight.Specular = aColor;
}
void Light::setSpecularLight(float aR, float aG, float aB, float aA)
{
	specColor.r = aR; specColor.g = aG; specColor.b = aB; specColor.a = aA;
	mLight.Specular = D3DXCOLOR(aR, aG, aB, aA);
}
void Light::setRange(float aRange)
{
	mRange = aRange;
	mLight.Range = aRange;
}
void Light::setPosition(float aX, float aY, float aZ)
{
	mLight.Position = D3DXVECTOR3(aX, aY, aZ);
}
void Light::setDirection(D3DXVECTOR3 aVec)
{
	mLight.Direction = aVec;
}
void Light::setDirection(float aX, float aY, float aZ)
{
	mLight.Direction = D3DXVECTOR3(aX, aY, aZ);
}
HRESULT Light::setLightToDevice(int index)
{
	//might need to change sfirst param, which is index of light properties
	return DirectXFramework::Instance()->getDevice()->SetLight(index, &mLight);
}
HRESULT Light::enableLight(int index, bool aBool)
{
	//might need to change first param, which is index of light properties
	return DirectXFramework::Instance()->getDevice()->LightEnable(index, aBool);
}
void Light::Update(Input * apInput, int aIndex)
{
#ifdef TESTING_LIGHTS
	/*************************************
	********* ambient color **************
	* increment */
	if(apInput->KeyDown(DIK_Q))
	{
		ambColor.r+=0.01f;
	}
	if(apInput->KeyDown(DIK_W))
	{
		ambColor.g+=0.01f;
	}
	if(apInput->KeyDown(DIK_E))
	{
		ambColor.b+=0.01f;
	}
	//increment all values of ambColor
	if(apInput->KeyDown(DIK_R))
	{
		ambColor.r+=0.01f;
		ambColor.g+=0.01f;
		ambColor.b+=0.01f;
	}
	//decrement
	if(apInput->KeyDown(DIK_A))
	{
		ambColor.r-=0.01f;
	}
	if(apInput->KeyDown(DIK_S))
	{
		ambColor.g-=0.01f;
	}
	if(apInput->KeyDown(DIK_D))
	{
		ambColor.b-=0.01f;
	}
	//decrement all values of ambColor
	if(apInput->KeyDown(DIK_F))
	{
		ambColor.r-=0.01f;
		ambColor.g-=0.01f;
		ambColor.b-=0.01f;
	}

	/*************************************
	********** diffuse color *************
	* increment */
	if(apInput->KeyDown(DIK_Y))
	{
		difColor.r+=0.01f;
	}
	if(apInput->KeyDown(DIK_U))
	{
		difColor.g+=0.01f;
	}
	if(apInput->KeyDown(DIK_I))
	{
		difColor.b+=0.01f;
	}
	if(apInput->KeyDown(DIK_O))
	{
		difColor.r+=0.01f;
		difColor.g+=0.01f;
		difColor.b+=0.01f;
	}
	//decrement
	if(apInput->KeyDown(DIK_H))
	{
		difColor.r-=0.01f;
	}
	if(apInput->KeyDown(DIK_J))
	{
		difColor.g-=0.01f;
	}
	if(apInput->KeyDown(DIK_K))
	{
		difColor.b-=0.01f;
	}
	if(apInput->KeyDown(DIK_L))
	{
		difColor.r-=0.01f;
		difColor.g-=0.01f;
		difColor.b-=0.01f;
	}


	/*************************************
	********** specular color *************
	* increment */
	if(apInput->KeyDown(DIK_NUMPAD7))
	{
		specColor.r+=0.01f;
	}
	if(apInput->KeyDown(DIK_NUMPAD8))
	{
		specColor.g+=0.01f;
	}
	if(apInput->KeyDown(DIK_NUMPAD9))
	{
		specColor.b+=0.01f;
	}
	if(apInput->KeyDown(DIK_ADD))
	{
		specColor.r+=0.01f;
		specColor.g+=0.01f;
		specColor.b+=0.01f;
	}
	//decrement
	if(apInput->KeyDown(DIK_NUMPAD4))
	{
		specColor.r-=0.01f;
	}
	if(apInput->KeyDown(DIK_NUMPAD5))
	{
		specColor.g-=0.01f;
	}
	if(apInput->KeyDown(DIK_NUMPAD6))
	{
		specColor.b-=0.01f;
	}
	if(apInput->KeyDown(DIK_SUBTRACT))
	{
		specColor.r-=0.01f;
		specColor.g-=0.01f;
		specColor.b-=0.01f;
	}


	//range
	//increment
	if(apInput->KeyDown(DIK_EQUALS))
	{
		mRange+=0.01f;
	}
	//decrement
	if(apInput->KeyDown(DIK_MINUS))
	{
		mRange-=0.01f;
	}



	//ask for forgiveness
	if(ambColor.r > 1.0f)
		ambColor.r = 1.0f;
	if(ambColor.g > 1.0f)
		ambColor.g = 1.0f;
	if(ambColor.b > 1.0f)
		ambColor.b = 1.0f;

	if(ambColor.r < 0.0f)
		ambColor.r = 0.0f;
	if(ambColor.g < 0.0f)
		ambColor.g = 0.0f;
	if(ambColor.b < 0.0f)
		ambColor.b = 0.0f;



	if(difColor.r > 1.0f)
		difColor.r = 1.0f;
	if(difColor.g > 1.0f)
		difColor.g = 1.0f;
	if(difColor.b > 1.0f)
		difColor.b = 1.0f;

	if(difColor.r < 0.0f)
		difColor.r = 0.0f;
	if(difColor.g < 0.0f)
		difColor.g = 0.0f;
	if(difColor.b < 0.0f)
		difColor.b = 0.0f;



	if(specColor.r > 1.0f)
		specColor.r = 1.0f;
	if(specColor.g > 1.0f)
		specColor.g = 1.0f;
	if(specColor.b > 1.0f)
		specColor.b = 1.0f;

	if(specColor.r < 0.0f)
		specColor.r = 0.0f;
	if(specColor.g < 0.0f)
		specColor.g = 0.0f;
	if(specColor.b < 0.0f)
		specColor.b = 0.0f;


	if(mRange > 2000.0f)
		mRange = 2000.0f;
	if(mRange < 0.0f)
		mRange = 0.0f;

	//set the light
	mLight.Ambient = ambColor;
	mLight.Diffuse = difColor;
	mLight.Specular = specColor;
	//send light info to device
	setLightToDevice(aIndex);
	DirectXFramework::Instance()->getDevice()->SetRenderState(D3DRS_AMBIENT, ambColor);

#endif
}